package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.PatronsFishBindingData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2021/10/21 20:52
 */
public class PatronsFishBindingDaoImpl extends TempDataObjectDao<PatronsFishBindingData> {
    public static String insertPatronsFishBindingDataSql = "insert into `t_u_patrons_fish_binding`(userId,fishId,patronsId,activityId,lastUpdateTime,systemId,systemEndTime) values (?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(PatronsFishBindingData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getFishId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPatronsId()));
        para.put(i++, new DbParameter(Types.NVARCHAR, JSON.toJSONString(data.getActivityIdList())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSystemId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getSystemEndTime()));
        if (getDb().executeNoneQuery(insertPatronsFishBindingDataSql, para) > 0) {
            return true;
        }
        return false;
    }
    public static String updatePatronsFishBindingDataSql = "update `t_u_patrons_fish_binding` set patronsId=?,activityId=?,lastUpdateTime=?,systemId=?,systemEndTime=? where fishId=? and userId = ?";
    @Override
    protected boolean updateData(PatronsFishBindingData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getPatronsId()));
        para.put(i++, new DbParameter(Types.NVARCHAR, JSON.toJSONString(data.getActivityIdList())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSystemId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getSystemEndTime()));

        para.put(i++, new DbParameter(Types.INTEGER, data.getFishId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updatePatronsFishBindingDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected PatronsFishBindingData parseData(ResultSet rs) throws SQLException {
        PatronsFishBindingData data = new PatronsFishBindingData();
        data.setUserId(rs.getLong("userId"));
        data.setFishId(rs.getInt("fishId"));
        data.setPatronsId(rs.getInt("patronsId"));
        String str = rs.getString("activityId");
        if (StringUtils.isNullOrEmpty(str)) {
            data.setActivityIdList(new ArrayList<>());
        }else {
            data.setActivityIdList(JSON.parseArray(str, Integer.class));
        }
        data.setLastUpdateTime(rs.getLong("lastUpdateTime"));
        data.setSystemId(rs.getInt("systemId"));
        data.setSystemEndTime(rs.getLong("systemEndTime"));

        return data;
    }
    public static String getPatronsFishBindingDataSql = "select * from `t_u_patrons_fish_binding` where `userId`=?";
    public Map<Integer, PatronsFishBindingData> getPatronsVehicleBindingData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, PatronsFishBindingData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getPatronsFishBindingDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                PatronsFishBindingData data = parse(rs);
                dataMap.put(data.getFishId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }
}
